package sgd.tp2.server.net.udp;

import java.io.IOException;
import java.net.DatagramSocket;

import sgd.tp2.api.logging.Log;
import sgd.tp2.api.net.CDRFormatType;
import sgd.tp2.api.net.CEPType;
import sgd.tp2.server.net.Server;

public abstract class UDPServer extends Server {

	protected DatagramSocket serverSocket = null;
	protected int packetSize = 1024;
	boolean running = true;

	public UDPServer(CDRFormatType cdrFormatType, int port, CEPType cepType) {
		super(cdrFormatType, port, cepType);
	}

	public UDPServer(CDRFormatType cdrFormatType, int port, CEPType cepType, int packetSize) {
		super(cdrFormatType, port, cepType);
		this.packetSize = packetSize;
	}

	@Override
	public void startServer() throws IOException {
		serverSocket = new DatagramSocket(port);
		Log.infoln("Iniciado servidor UDP:{0} (Engine: {1}) com formato {2}.", port, cepType,
				cdrFormatType);

		Log.info("A receber: ");
	}

	@Override
	public void stopServer() {
		running = false;

		if (serverSocket != null && !serverSocket.isClosed())
			serverSocket.close();
		serverSocket = null;

		Log.infoln("Servidor terminado.");
	}
}
